package xiaoka.ecarx.com.cn.youlong.player;

import android.util.Log;

import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.CommonRequest;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.AlbumList;
import com.ximalaya.ting.android.opensdk.model.track.TrackList;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import xiaoka.ecarx.com.cn.youlong.YouLongApplication;

/**
 * Created by zhanghao on 2016/11/1.
 */

public class PlayerDataProvider {

    public static final String TAG = PlayerDataProvider.class.getName();

    public static final int NORMAL_SOURCE = 0;//默认的正常资源

    public static final int TEMPORARY_SOURCE = 1;//临时资源

    private int currentPlayerSourceStatus;//当前播放资源的状态

    private Map<String, TrackList> trackListMap = new HashMap<>();//每个专辑对应的TrackList数据

    private String currentPlayAlbumId;//当前播放的专辑id;

    private PlayerTrackListStatus playerTrackListStatus;

    private LinkedList<String> defalutAlbumIds = new LinkedList<>();//默认播放的专辑Id列表

    private static PlayerDataProvider instance;

    public static PlayerDataProvider getInstance() {
        if (instance == null) {
            instance = new PlayerDataProvider();
        }
        return instance;
    }

    private PlayerDataProvider() {
        loadAlbumData();
    }

    /**
     * 获取下一张专辑id
     *
     * @return
     */
    private String getNextAlbumId() {
        if (currentPlayAlbumId == null) {
            return defalutAlbumIds.peek();
        } else {
            int index = defalutAlbumIds.indexOf(currentPlayAlbumId);
            if (index + 1 < defalutAlbumIds.size()) {
                return defalutAlbumIds.get(index + 1);
            } else {
                return defalutAlbumIds.getFirst();
            }
        }
    }

    public void setTrackListMap(Map<String, TrackList> trackListMap) {
        this.trackListMap = trackListMap;
    }

    public void setCurrentPlayerSourceStatus(int currentPlayerSourceStatus) {
        this.currentPlayerSourceStatus = currentPlayerSourceStatus;
    }

    /**
     * 切换专辑
     */
    public void switchAlbum() {
        if (currentPlayerSourceStatus == NORMAL_SOURCE) {//如果当前播放的默认列表的资源，切换专辑是下一个专辑
            playTrackListByAlumId(getNextAlbumId());
        } else {//如果当前播放的是临时推送的资源，点击切换专辑是当前专辑
            currentPlayerSourceStatus = NORMAL_SOURCE;
            ClientPlayerManager.getInstance(YouLongApplication.getApplication()).setPlayList(playerTrackListStatus.trackList, playerTrackListStatus.trackIndex);
            ClientPlayerManager.getInstance(YouLongApplication.getApplication()).seekTo(playerTrackListStatus.position);
            ClientPlayerManager.getInstance(YouLongApplication.getApplication()).play();
        }
    }

    /**
     * 播放某专辑id下的trackList;
     *
     * @param albumId
     */
    public void
    playTrackListByAlumId(String albumId) {
        TrackList trackList = null;
        if (trackListMap.containsKey(albumId)) {
            trackList = trackListMap.get(albumId);
            ClientPlayerManager.getInstance(YouLongApplication.getApplication()).playList(trackList, 0);
            this.currentPlayAlbumId = albumId;
        } else {
            loadTrackListByAlbumId(albumId);
        }
    }

    /**
     * 根据专辑ID拿到TrackList并播放
     *
     * @param albumId
     */
    public void loadTrackListByAlbumId(final String albumId) {
        Map<String, String> map = new HashMap<>();
        map.put(DTransferConstants.ALBUM_ID, albumId + "");
        map.put(DTransferConstants.SORT, "asc");
        CommonRequest.getTracks(map, new IDataCallBack<TrackList>() {
            @Override
            public void onSuccess(TrackList trackList) {
                if (trackList != null) {
                    Log.d(TAG, "get data trackList=" + trackList.toString());
                    PlayerDataProvider.this.currentPlayAlbumId = albumId;
                    trackListMap.put(albumId, trackList);
                    ClientPlayerManager.getInstance(YouLongApplication.getApplication()).playList(trackList, 0);
                }
            }

            @Override
            public void onError(int i, String s) {
                Log.e(TAG, "get data trackList error: " + s);
            }
        });
    }

    public void loadAlbumData() {
        Map<String, String> map = new HashMap<>();
        map.put(DTransferConstants.CATEGORY_ID, "1");
        map.put(DTransferConstants.CALC_DIMENSION, "1");
        CommonRequest.getAlbumList(map, new IDataCallBack<AlbumList>() {
            @Override
            public void onSuccess(AlbumList albumList) {
                if (albumList != null && albumList.getTotalCount() > 0) {
                    Log.d(TAG, "get data albumelist =" + albumList.toString());
                    for (Album album : albumList.getAlbums()) {
                        defalutAlbumIds.add(album.getId() + "");
                    }
                    switchAlbum();
                }
            }

            @Override
            public void onError(int i, String s) {
                Log.e(TAG, "get data album error: " + s);
            }
        });
    }

    public void saveCurrentPlayList(int indexOfTrack, int position) {
        playerTrackListStatus = new PlayerDataProvider.PlayerTrackListStatus();
        playerTrackListStatus.albumId = currentPlayAlbumId;
        playerTrackListStatus.trackList = trackListMap.get(currentPlayAlbumId);
        playerTrackListStatus.trackIndex = indexOfTrack;
        playerTrackListStatus.position = position;
    }

    /**
     * 播放器某个时刻的状态
     */
    public static class PlayerTrackListStatus {
        public String albumId;//专辑id
        public TrackList trackList;//当前播放的TrackList;
        public int trackIndex;//当前播放的track在tracklist的位置
        public int position;//当前track播放到的位置position
    }

}
